package com.schoolcloud.system.service.impl;

import com.schoolcloud.common.model.PageDTO;
import com.schoolcloud.system.entity.SysRole;
import com.schoolcloud.system.mapper.SysRoleMapper;
import com.schoolcloud.system.query.SysRoleQuery;
import com.schoolcloud.system.service.SysRoleService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author hongmizfb
 * @description 针对表【sys_role(角色表)】的数据库操作Service实现
 * @createDate 2024-10-19 23:05:49
 */
@Service
@RequiredArgsConstructor
public class SysRoleServiceImpl implements SysRoleService {
    private final SysRoleMapper sysRoleMapper;

    @Override
    public List<String> getRoleCodeListByUser(String userId) {
        return sysRoleMapper.getRoleCodeListByUser(userId);
    }

    @Override
    public boolean isAdmin(String user) {
        List<String> list = getRoleCodeListByUser(user);
        if (list!=null){
            return list.contains("admin");
        }
        return false;
    }

    @Override
    public PageDTO<SysRole> getRolePage(SysRoleQuery query) {
        PageDTO<SysRole> dto = new PageDTO<>();
        List<SysRole> list = sysRoleMapper.getRolePage(query);
        System.out.println("查询结果"+list);
        long total = sysRoleMapper.getRoleCount(query);
        dto.setList(list);
        dto.setTotal(total);
        dto.setPageNum(query.getPageNum());
        return dto;
    }

    @Override
    public SysRole getRoleById(String roleId) {
        return sysRoleMapper.getRoleById(roleId);
    }

    @Override
    public void createSysRole(SysRole sysRole) {
        sysRoleMapper.addSysRole(sysRole);
    }

    @Override
    public void modifySysRole(SysRole role) {
        sysRoleMapper.modifySysRole(role);
    }
}
